package com.levelup.cr.web.controller;

import com.levelup.cr.model.Node;
import com.levelup.cr.model.entity.Product;
import com.levelup.cr.services.NodeChanger;
import com.levelup.cr.services.RepositoryResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created with IntelliJ IDEA.
 * User: denis_zavadsky
 * Date: 8/29/13
 * Time: 8:16 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private RepositoryResolver repositoryResolver;

    @Autowired
    private NodeChanger nodeChanger;

    public void setRepositoryResolver(RepositoryResolver repositoryResolver) {
        this.repositoryResolver = repositoryResolver;
    }

    public void setNodeChanger(NodeChanger nodeChanger) {
        this.nodeChanger = nodeChanger;
    }

    @RequestMapping("/get/{nodeId}")
    public ModelAndView getProduct(@PathVariable("nodeId") String id){

        Node node = repositoryResolver.findNodeById(id);
        Product product = node.adaptTo(Product.class);
        ModelAndView modelAndView = new ModelAndView("product");
        modelAndView.addObject("product",product);
        return modelAndView;

    }

    @RequestMapping("/add/{nodeName}/{parentId}")
    public Node addNode(@PathVariable String nodeName, @PathVariable String parentId) {
        Node node = nodeChanger.create();
        return node;
    }

}
